#include <gtest/gtest.h>
#include <string>
#include <unordered_map>

using namespace ::std;

// map存储字母与数字的对应关系
// 观察特殊情况的规律得到应对方法
class Solution {
public:
    int romanToInt(string s) {
		unordered_map<char, int> map = {
			{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50},
			{'C', 100}, {'D', 500}, {'M', 1000}
		};
		int result = 0, lastNum = 1000;
		for(int i = 0; i < s.size(); ++i) {
			int curNum = map[s[i]];
			if(curNum > lastNum) result -= 2 * lastNum; 
			result += curNum;
			lastNum = curNum;
		}
		return result;
    }
};

class RomanToIntCheck : public ::testing::Test {
public:
	class Solution solution;
};

TEST_F(RomanToIntCheck, normalCase_1) {
	string s = "III";
	EXPECT_EQ(3, solution.romanToInt(s));
}

TEST_F(RomanToIntCheck, normalCase_2) {
	string s = "LVIII";
	EXPECT_EQ(58, solution.romanToInt(s));
}

TEST_F(RomanToIntCheck, specialCase_1) {
	string s = "IX";
	EXPECT_EQ(9, solution.romanToInt(s));
}

TEST_F(RomanToIntCheck, specialCase_2) {
	string s = "MCMXCIV";
	EXPECT_EQ(1994, solution.romanToInt(s));
}

int main() {
	::testing::InitGoogleTest();
	return RUN_ALL_TESTS();
}
